#include "DDZPlayer.h"
#include "DDZSoundFun.h"
#include "DDZGameLogic.h"

static const char* const kCallStateImgs[8] = {
	"jiaodizhu.png", "qiangdizhu.png", "bujiao.png", "buqiang.png",
	"jiao1fen.png", "jiao2fen.png", "jiao3fen.png", "bujiao.png"
};

DDZPlayer::DDZPlayer(int iIdex, cocos2d::Node* pSeatNode)
	:GamePlayer(NULL)
	, m_iIdex(iIdex)
	, m_pSeatNode(pSeatNode)
	, m_bGameRecord(false)
{
	init();
	m_Self = false;
	m_ActionLayout = nullptr;
}

DDZPlayer::~DDZPlayer()
{

}

void DDZPlayer::init()
{
	PlayerLeave();
	if (m_ActionLayout == nullptr)
	{
		m_ActionLayout = cocos2d::Layer::create();
		m_pSeatNode->addChild(m_ActionLayout);
	}
}
int  DDZPlayer::getIdex()
{
	return m_iIdex;
}
void DDZPlayer::defaultState()
{
	clearHandCard();
	clearOutCard();
	showClock(false, 0);
	showFangZhu(false);
	showOffline(false);
	showReady(false);
	showBaoDan(false);
	showPass(false);
	showDizhu(false);
	m_kTouchCardList.clear();
	m_kSendCardList.clear();

	upPlayerState();
	if (m_ActionLayout == nullptr)
	{
		m_ActionLayout = cocos2d::Layer::create();
		m_pSeatNode->addChild(m_ActionLayout);
	}
}

void DDZPlayer::setGameRecord(bool bRecord)
{
	m_bGameRecord = bRecord;
}


void DDZPlayer::PlayerEnter()
{
	WidgetFun::setVisible(m_pSeatNode, "Name", true);
	WidgetFun::setVisible(m_pSeatNode, "GoldImagic", true);
	WidgetFun::setVisible(m_pSeatNode, "HeadImagicEmpty", false);
	WidgetFun::setVisible(m_pSeatNode, "HeadImagic", true);

	if (WidgetFun::getChildWidget(m_pSeatNode, "ButtonPlayerHeadClick"))
	{
		WidgetFun::setWidgetUserInfo(m_pSeatNode, "ButtonPlayerHeadClick", "UserID", utility::toString((int)GetUserID()));
	}
	if (m_ActionLayout == nullptr)
	{
		m_ActionLayout = cocos2d::Layer::create();
		m_pSeatNode->addChild(m_ActionLayout);
	}
}

void DDZPlayer::startGameNoTime()
{
	WidgetFun::runWidgetAction(m_pSeatNode, "Head", "StartNoTime");
	WidgetFun::setVisible(m_pSeatNode, "ReadyState", false);
}

void DDZPlayer::PlayerLeave()
{
	defaultState();
	WidgetFun::setVisible(m_pSeatNode, "HeadImagicEmpty", true);
	WidgetFun::setVisible(m_pSeatNode, "HeadImagic", false);
	WidgetFun::setVisible(m_pSeatNode, "Name", false);
	WidgetFun::setVisible(m_pSeatNode, "GoldImagic", false);
	WidgetFun::setText(m_pSeatNode, "Name", "");
	WidgetFun::setText(m_pSeatNode, "GoldTxt", "");
}

cocos2d::Node* DDZPlayer::getPlayerNode()
{
	return m_pSeatNode;
}

void DDZPlayer::upPlayerInfo()
{
	WidgetFun::setText(m_pSeatNode, "Name", GetNickName());
	if (GetHeadHttp() != "")
	{
		ImagicDownManager::Instance().addDown(WidgetFun::getChildWidget(m_pSeatNode, "HeadImagic"),
			GetHeadHttp(), GetUserID());
	}
}

void DDZPlayer::hide(bool istrue)
{

	WidgetFun::setVisible(m_pSeatNode, "Head", !istrue);
	WidgetFun::setVisible(m_pSeatNode, "Name", !istrue);
	WidgetFun::setVisible(m_pSeatNode, "GoldImagic", !istrue);
	WidgetFun::setVisible(m_pSeatNode, "Name", !istrue);
	WidgetFun::setVisible(m_pSeatNode, "GoldTxt", !istrue);
}

void DDZPlayer::setZhuang()
{
	WidgetFun::setVisible(m_pSeatNode, "Zhuang", true);
}
void DDZPlayer::showStatusImagic(std::string kKey)
{
	WidgetFun::setPlaceImagicKey(m_pSeatNode, "ReadyState", kKey);
	showReady(true);
}

void DDZPlayer::showTalkState(CMD_GR_C_TableTalk* pNetInfo)
{
	if (pNetInfo->cbChairID != GetChairID())
	{
		return;
	}
	
	//if (DDZGameLogic::Instance().lastTalk->strTalkSize >= 1 && DDZGameLogic::Instance().lastTalk->cbChairID != GetChairID())
	//{
	//	return;
	//}
	//if (DDZGameLogic::Instance().lastTalk->strTalkSize <= 0 && DDZGameLogic::Instance().lastTalk->cbChairID == GetChairID())
	//{
	//	return;
	//}
	//if (DDZGameLogic::Instance().lastTalk->cbChairID == GetChairID())
	//{
	//	if (pNetInfo->cbType != DDZGameLogic::Instance().lastTalk->cbType)
	//	{
	//		return;
	//	}
	//	if (DDZGameLogic::Instance().lastTalk->cbType == CMD_GR_C_TableTalk::TYPE_WORD || DDZGameLogic::Instance().lastTalk->cbType == CMD_GR_C_TableTalk::TYPE_BIAOQING || DDZGameLogic::Instance().lastTalk->cbType == CMD_GR_C_TableTalk::TYPE_DEFINE)
	//	{
	//		std::string laststr = DDZGameLogic::Instance().lastTalk->strString;
	//		std::string curstr = pNetInfo->strString;
	//		if (laststr != curstr)
	//		{
	//			return;
	//		}
	//	}

	//	if (DDZGameLogic::Instance().lastTalk->cbType == CMD_GR_C_TableTalk::TYPE_FILE)
	//	{
	//		if (DDZGameLogic::Instance().lastTalk->strTalkSize != pNetInfo->strTalkSize)
	//		{
	//			return;
	//		}
	//	}
	//}
	//if (m_Self == true)
	//{

	//}
	//else
	//{
	//	if (DDZGameLogic::Instance().lastTalk2->strTalkSize > 0 || pNetInfo->cbType == DDZGameLogic::Instance().lastTalk2->cbType)
	//	{
	//		int same = 0;
	//		if (pNetInfo->cbType == DDZGameLogic::Instance().lastTalk2->cbType)
	//		{
	//			//return;
	//			same++;
	//		}
	//		if (DDZGameLogic::Instance().lastTalk2->cbType == CMD_GR_C_TableTalk::TYPE_WORD || DDZGameLogic::Instance().lastTalk2->cbType == CMD_GR_C_TableTalk::TYPE_BIAOQING || DDZGameLogic::Instance().lastTalk->cbType == CMD_GR_C_TableTalk::TYPE_DEFINE)
	//		{
	//			std::string laststr = DDZGameLogic::Instance().lastTalk2->strString;
	//			std::string curstr = pNetInfo->strString;
	//			if (laststr == curstr)
	//			{
	//				//return;
	//				same++;
	//			}
	//		}

	//		if (DDZGameLogic::Instance().lastTalk2->cbType == CMD_GR_C_TableTalk::TYPE_FILE)
	//		{
	//			if (DDZGameLogic::Instance().lastTalk2->strTalkSize == pNetInfo->strTalkSize)
	//			{
	//				//return;
	//				same++;
	//			}
	//		}
	//		if (same >= 2)
	//		{
	//			return;
	//		}
	//	}

	//	DDZGameLogic::Instance().lastTalk2->cbType = pNetInfo->cbType;
	//	//DDZGameLogic::Instance().lastTalk2->strString = pNetInfo->strString;
	//	std::string curstr = pNetInfo->strString;
	//	utility::StringToChar(curstr, DDZGameLogic::Instance().lastTalk2->strString, 128);
	//	DDZGameLogic::Instance().lastTalk2->strTalkSize = pNetInfo->strTalkSize;
	//}

	//auto seq = cocos2d::Sequence::create(
	//	cocos2d::DelayTime::create(0.05),
	//	cocos2d::CallFunc::create([]() {
	//	DDZGameLogic::Instance().lastTalk2->strString[0] = '1';
	//	DDZGameLogic::Instance().lastTalk2->strTalkSize = -1;
	//}),
	//	NULL
	//	);
	//m_ActionLayout->stopAllActions();
	//m_ActionLayout->runAction(seq);
	CCLOG("talk chair Id = %d", (int)pNetInfo->cbChairID);
	std::string kStrNet = pNetInfo->strString;
	if (pNetInfo->cbType == CMD_GR_C_TableTalk::TYPE_FILE)
	{
		if (!WidgetFun::getChildWidget(m_pSeatNode, "TalkFile"))
		{
			return;
		}
		WidgetFun::runWidgetAction(m_pSeatNode, "TalkFile", "Start");
	}
	if (pNetInfo->cbType == CMD_GR_C_TableTalk::TYPE_WORD)
	{
		if (!WidgetFun::getChildWidget(m_pSeatNode, "TalkTxtNode"))
		{
			return;
		}
		utility::runPaoMaDeng(m_pSeatNode, "TalkTxt", "LayoutNode",
			kStrNet, NULL);
		cocos2d::Node* pTalkNode = WidgetFun::getChildWidget(m_pSeatNode, "TalkTxtNode");
		pTalkNode->setVisible(true);
		pTalkNode->stopAllActions();
		pTalkNode->runAction(cocos2d::CCVisibleAction::create(6.0f, false));
	}
	if (pNetInfo->cbType == CMD_GR_C_TableTalk::TYPE_BIAOQING)
	{
		if (!WidgetFun::getChildWidget(m_pSeatNode, "BiaoQingNode"))
		{
			return;
		}
		WidgetFun::setImagic(m_pSeatNode, "BiaoQingPic", kStrNet);
		cocos2d::Node* pTalkNode = WidgetFun::getChildWidget(m_pSeatNode, "BiaoQingNode");
		pTalkNode->setVisible(true);
		pTalkNode->stopAllActions();
		pTalkNode->runAction(cocos2d::CCVisibleAction::create(4.0f, false));
	}
	if (pNetInfo->cbType == CMD_GR_C_TableTalk::TYPE_DEFINE)
	{
		if (!WidgetFun::getChildWidget(m_pSeatNode, "TalkFile"))
		{
			return;
		}
		std::vector<std::string> kStr = utility::split(kStrNet, ":");

		ASSERT(kStr.size() == 2);

		WidgetFun::runWidgetAction(m_pSeatNode, "TalkFile", "Start");

		SoundFun::Instance().playEffect(kStr[0]);

		utility::runPaoMaDeng(m_pSeatNode, "TalkTxt", "LayoutNode",
			kStr[1], NULL);
		cocos2d::Node* pTalkNode = WidgetFun::getChildWidget(m_pSeatNode, "TalkTxtNode");
		pTalkNode->setVisible(true);
		pTalkNode->stopAllActions();
		pTalkNode->runAction(cocos2d::CCVisibleAction::create(6.0f, false));
	}
}

void DDZPlayer::upPlayerState()
{
	if (GetUserStatus() == US_READY)
	{
		showStatusImagic("Ready");
	}
	WidgetFun::setVisible(m_pSeatNode, "Offline", GetUserStatus() == US_OFFLINE);
}

void DDZPlayer::showClock(bool bShow, int nTime)
{
	cocos2d::Node* pClock = WidgetFun::getChildWidget(m_pSeatNode, "PlayerClock");
	pClock->setVisible(bShow);
	cocos2d::Node* pClockTime = WidgetFun::getChildWidget(pClock, "ClockTime");
	pClockTime->stopAllActions();
	pClockTime->runAction(cocos2d::MoveExTxtTime::create(nTime, nTime, 0));
}

void DDZPlayer::showFangZhu(bool bShow)
{
	WidgetFun::setVisible(m_pSeatNode, "Head:FangZhu", bShow);
}

void DDZPlayer::showOffline(bool bShow)
{
	WidgetFun::setVisible(m_pSeatNode, "Head:Offline", bShow);
}

void DDZPlayer::showBaoDan(bool bShow)
{
	if (bShow)
		DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_LEFT1);
	WidgetFun::setVisible(m_pSeatNode, "Head:BaoDan", bShow);
}

void DDZPlayer::showDizhu(bool bShow)
{
	WidgetFun::setVisible(m_pSeatNode, "Head:Dizhu", bShow);
}

void DDZPlayer::showReady(bool bShow)
{
	WidgetFun::setVisible(m_pSeatNode, "ReadyState", bShow);
}

void DDZPlayer::startGame(SCORE lScores)
{
	lScore = (int)lScores;
	WidgetFun::setText(m_pSeatNode, "GoldTxt", lScore);
	showReady(false);
	clearOutState();
}

void DDZPlayer::setScore(SCORE lScores)
{
	lScore = (int)lScores;
	WidgetFun::setText(m_pSeatNode, "GoldTxt", lScore);
}

void DDZPlayer::showPass(bool bShow)
{
	WidgetFun::setVisible(m_pSeatNode, "PlayerPass", bShow);
	if (bShow)
	{
		showClock(false, 0);
		DDZSoundFun::playPassByGender(GetGender());
	}
}

void DDZPlayer::showSkipRocket()
{
	if (WidgetFun::getChildWidget(m_pSeatNode, "PlayerSkip"))
	{
		WidgetFun::runWidgetAction(m_pSeatNode, "PlayerSkip", "Start");
	}
}

void DDZPlayer::showCallState(int nCallState)
{
	CCAssert(nCallState >= 0 && nCallState <= 8, "Invalide CallState");
	if (nCallState)
	{
		WidgetFun::setImagic(m_pSeatNode, "PlayerCall", utility::toString("ddzroom/", kCallStateImgs[nCallState - 1]));
		DDZSoundFun::playCallStateByGender(GetGender(), nCallState, nCallState == PLAYER_ROB_RAISE);
	}
	WidgetFun::setVisible(m_pSeatNode, "PlayerCall", nCallState);
}

void DDZPlayer::clearOutState()
{
	showClock(false, 0);
	showPass(false);
	clearOutCard();
}

void DDZPlayer::runAniByCards(const std::vector<BYTE>& kCardList)
{
	int MaxValue = -1;
	int nType = DDZGameLogic::Instance().GetCardType(kCardList, MaxValue);
	int nCardNum = kCardList.size();
	switch (nType)
	{
	case CT_SINGLE:
	{
		DDZSoundFun::playCardByGender(GetGender(), (int)kCardList[0]);
		break;
	}
	case CT_DOUBLE:
	{
		if (MaxValue == DDZGameLogic::Instance().GetCardLogicValue(kCardList[0]))
		{
			DDZSoundFun::playCardByGender_Pair(GetGender(), (int)kCardList[0]);
		}
		else
		{
			DDZSoundFun::playCardByGender_Pair(GetGender(), (int)kCardList[1]);
		}
		
		break;
	}
	case CT_THREE:
	{
		//DDZSoundFun::playCardByGender_Tuple(GetGender(), (int)kCardList[0]);
		if (MaxValue == DDZGameLogic::Instance().GetCardLogicValue(kCardList[0]))
		{
			DDZSoundFun::playCardByGender_Pair(GetGender(), (int)kCardList[0]);
		}
		else if (MaxValue == DDZGameLogic::Instance().GetCardLogicValue(kCardList[1]))
		{
			DDZSoundFun::playCardByGender_Pair(GetGender(), (int)kCardList[1]);
		}else
		{
			DDZSoundFun::playCardByGender_Pair(GetGender(), (int)kCardList[2]);
		}
		break;
	}
	case CT_SINGLE_LINE:
	{
		runShunZiAni();
		break;
	}
	case CT_DOUBLE_LINE:
	{
		runLianDuiAni();
		break;
	}
	case CT_THREE_LINE:
	{
		runFeiJiAni();
		break;
	}
	case CT_THREE_TAKE_ONE:
	{
		if (nCardNum < 5)
		{
			DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_3TAKE1);
		}
		else
		{
			runFeiJiAni();
		}
		break;
	}
	case CT_THREE_TAKE_TWO:
	{
		if (nCardNum < 6)
		{
			DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_3TAKE2);
		}
		else
		{
			runFeiJiAni();
		}
		break;
	}
	case CT_FOUR_TAKE_ONE:
	{
		DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_4TAKE2);
		break;
	}
	case CT_FOUR_TAKE_TWO:
	{
		DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_4TAKE22);
		break;
	}
	case CT_BOMB_CARD:
	{
		runBombAni();
		break;
	}
	case CT_MISSILE_CARD:
	{
		runRocketAni();
		break;
	}
	default:
		break;
	}
}

void DDZPlayer::runBombAni()
{
	cocos2d::Node* AnimateNode = WidgetFun::getChildWidget(m_pSeatNode, "AnimateNode");
	AnimateNode->removeAllChildren();
	WidgetManager::Instance().createWidget("BombAni", AnimateNode);

	DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_BOMB);
}
void DDZPlayer::runRocketAni()
{
	cocos2d::Node* AnimateNode = WidgetFun::getChildWidget(m_pSeatNode, "AnimateNode");
	AnimateNode->removeAllChildren();
	WidgetManager::Instance().createWidget("BombAni", AnimateNode);

	DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_ROCKET);
}
void DDZPlayer::runLianDuiAni()
{
	cocos2d::Node* AnimateNode = WidgetFun::getChildWidget(m_pSeatNode, "AnimateNode");
	AnimateNode->removeAllChildren();
	WidgetManager::Instance().createWidget("LianDuiAni", AnimateNode);

	DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_PAIRS);
}
void DDZPlayer::runShunZiAni()
{
	cocos2d::Node* AnimateNode = WidgetFun::getChildWidget(m_pSeatNode, "AnimateNode");
	AnimateNode->removeAllChildren();
	WidgetManager::Instance().createWidget("ShunZiAni", AnimateNode);

	DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_SHUNZI);
}
void DDZPlayer::runFeiJiAni()
{
	cocos2d::Node* AnimateNode = WidgetFun::getChildWidget(m_pSeatNode, "AnimateNode");
	AnimateNode->removeAllChildren();
	WidgetManager::Instance().createWidget("FeiJiAni", AnimateNode);

	DDZSoundFun::playEffectByGender(GetGender(), DDZSoundFun::EFFECT_FEIJI);
}
